#!/usr/bin/env bash
# shellcheck shell=bash

[ ! -z "$DEBUG" ] && set -x

# Default values
PSQL_VERSION="15"
PORTNO="@PGSQL_DEFAULT_PORT@"
PSQL_USER="postgres"

# Function to display help
print_help() {
    echo "Usage: nix run .#start-client -- [options]"
    echo
    echo "Options:"
    echo "  -v, --version [15|16|orioledb-16]  Specify the PostgreSQL version to use (required)"
    echo "  -u, --user USER                    Specify the user/role to use (default: postgres)"
    echo "  -h, --help                         Show this help message"
    echo
    echo "Description:"
    echo "  Starts an interactive 'psql' session connecting to a Postgres database started with the"
    echo "  'nix run .#start-server' command. If a migration file is not provided, the client"
    echo "  initializes the database with the default migrations for a new Supabase project."
    echo "  If a migrations file is provided, default migrations are skipped"
    echo "  If no migration file is provided, it runs the default Supabase migrations."
    echo
    echo "Examples:"
    echo "  nix run .#start-client"
    echo "  nix run .#start-client -- --version 15"
    echo "  nix run .#start-client -- --version 16 --port 5433"
    echo "  nix run .#start-client -- --version 16 --user supabase_admin"
}

# Parse arguments
while [[ "$#" -gt 0 ]]; do
    case "$1" in
        -v|--version)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PSQL_VERSION="$2"
                shift 2
            else
                echo "Error: --version requires an argument (15, 16, or orioledb-16)"
                exit 1
            fi
            ;;
        -u|--user)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PSQL_USER="$2"
                shift 2
            else
                echo "Error: --user requires an argument"
                exit 1
            fi
            ;;
        -p|--port)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PORTNO="$2"
                shift 2
            else
                echo "Error: --port requires an argument"
                exit 1
            fi
            ;;
        -h|--help)
            print_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            print_help
            exit 1
            ;;
    esac
done

# Check if version is provided
if [[ -z "$PSQL_VERSION" ]]; then
    echo "Error: PostgreSQL version is required."
    print_help
    exit 1
fi

# Determine PostgreSQL version
if [ "$PSQL_VERSION" == "15" ]; then
    echo "Starting client for PSQL 15"
    PSQL15=@PSQL15_BINDIR@
    BINDIR="$PSQL15"
elif [ "$PSQL_VERSION" == "17" ]; then
    echo "Starting client for PSQL 17"
    PSQL17=@PSQL17_BINDIR@
    BINDIR="$PSQL17"
elif [ "$PSQL_VERSION" == "orioledb-17" ]; then
    echo "Starting client for PSQL ORIOLEDB 17"
    PSQLORIOLEDB17=@PSQLORIOLEDB17_BINDIR@
    BINDIR="$PSQLORIOLEDB17"
else
    echo "Please provide a valid Postgres version (15, 17, or orioledb-17)"
    exit 1
fi

#vars for migration.sh
export PATH=$BINDIR/bin:$PATH
export POSTGRES_DB=postgres
export POSTGRES_HOST=localhost

# Start interactive psql session
exec psql -U "$PSQL_USER" -p "$PORTNO" -h localhost postgres
